package com.manager.system.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.System_Organization;
import com.manager.model.System_Organization_Permission;
import com.manager.model.System_Permission;
import com.manager.system.service.OrganizationService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Component
@SuppressWarnings("all")
public class OrganizationServiceImpl extends SuperDao<Object> implements OrganizationService{

	@Override
	@Transactional
	public PageVO getOrgMap(System_Organization org) {
		// TODO Auto-generated method stub
		List<Object> params = new ArrayList<Object>();
		
		String sql = "select t  ";
		String sql2 = " from System_Organization t where 1 = 1 ";
		if(StringUtils.isNotEmpty(org.getName())){
			sql2 += " and t.name like ?1 ";
			params.add("%" + org.getName() + "%");
		}
		
		List<System_Organization> list = this.getList(sql + sql2, params.toArray(), 0,0, "");
		JSONArray jsonArray = ObjectUtils.getJSONArray(System_Organization.config, list);
		int sum = this.getListCount( "select count(t.id) " + sql2, params.toArray(), "");
		int count = (int) Math.ceil((Double.valueOf(sum)/Double.valueOf(org.getLimit())));

		
		return PageVO.create(org.getStart(),count,jsonArray);
	}

	@Override
	@Transactional
	public void addOrg(System_Organization organization) {
		// TODO Auto-generated method stub
		organization.setId(UUID.randomUUID().toString().replace("-", ""));
		this.addT(organization);
		
		String menuIds = organization.getString1();
		if(StringUtils.isNotEmpty(menuIds)){
			String[] ids = menuIds.split(",");
			for(String id:ids){
				System_Organization_Permission system_Organization_Permission = new System_Organization_Permission();
				System_Permission system_Permission = (System_Permission) this.getT(new System_Permission(), id);
				system_Organization_Permission.setSys_orga(organization);
				system_Organization_Permission.setSys_perm(system_Permission);
				this.addT(system_Organization_Permission);
				
			}
		}
	}

	@Override
	@Transactional
	public void updateOrg(System_Organization organization) {
		// TODO Auto-generated method stub
		System_Organization org = (System_Organization) this.getT(new System_Organization(), organization.getId());
		
		BeanUtils.copyProperties(organization, org, new String[]{"id","listuo","listop","sys_user"});
		
		this.batchDelete(org.getListop());
		org.getListop().clear();
		
		this.updateT(org);
		
		String menuIds = organization.getString1();
		if(StringUtils.isNotEmpty(menuIds)){
			String[] ids = menuIds.split(",");
			for(String id:ids){
				System_Organization_Permission system_Organization_Permission = new System_Organization_Permission();
				System_Permission system_Permission = (System_Permission) this.getT(new System_Permission(), id);
				system_Organization_Permission.setSys_orga(org);
				system_Organization_Permission.setSys_perm(system_Permission);
				this.addT(system_Organization_Permission);
				
			}
		}
	}

	@Override
	@Transactional
	public void deleteOrg(System_Organization organization) {
		// TODO Auto-generated method stub
		System_Organization org = (System_Organization) this.getT(new System_Organization(), organization.getId());
		this.delete(org);
	}

	@Override
	@Transactional
	public JSONArray getOrgList() {
		// TODO Auto-generated method stub
		String sql = "from System_Organization t ";
		List<System_Organization> list = this.getList(sql, 0, 0, "");
		
		return ObjectUtils.getJSONArray(System_Organization.config, list);
	}
	@Override
	@Transactional
	public JSONArray getMenuTree(System_Organization model) {
		// TODO Auto-generated method stub
		String sql = "from System_Permission t ";
		List<System_Permission> list = this.getList(sql, 0, 0, " order by t.sort ");
		JSONArray jsonArray = ObjectUtils.getJSONArray(System_Permission.config, list);
		sql = "from System_Organization_Permission t where t.sys_orga.id = ?1 ";
		List<System_Organization_Permission> list2 = this.getList(sql, new Object[]{model.getId()}, 0, 0);	
		
		for(int i = 0;i < jsonArray.size();i++){
			JSONObject json = jsonArray.getJSONObject(i);
			for(System_Organization_Permission role_Permission:list2){
				if(json.getString("id").equals(role_Permission.getSys_perm().getId())){
					json.put("checked", true);
				}
			}
			
		}
		
		return jsonArray;
	}

	@Override
	public List<System_Permission> getPermissionsByOrgId(String orgId) {
		String sql = "select t from System_Permission t left join t.listop to where to.sys_orga.id = ?1 ";
		List<System_Permission> list = this.getList(sql, new Object[]{orgId}, 0, 0);
		return list;
	}

	@Override
	public List<System_Organization> getAllOrg() {
		String sql = " from System_Organization t ";
		List<System_Organization> list = this.getList(sql, 0, 0, "");
		return list;
	}
}
